/**
 * 
 */
package scriptrunner.execution;

import java.io.Serializable;

/**
 * @author Administrator
 * 
 */
public class CpNotEqualExpr extends CompareExpr implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2950663921091029034L;

	public CpNotEqualExpr(EvaluateExpr eeLeft, EvaluateExpr eeRight) {
		this.eeLeft = eeLeft;
		this.eeRight = eeRight;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * scriptrunner.execution.CompareExpr#CalcluateResult(scriptrunner.execution
	 * .Session)
	 */
	@Override
	public boolean CalculateResult(Session se) throws ExecuteErrorException {
		ValueObject left = eeLeft.CalculateResult(se);
		ValueObject right = eeRight.CalculateResult(se);

		if (left instanceof EqualAndNotEqual
				&& right instanceof EqualAndNotEqual) {
			// Same Class
			if (left.getClass() == right.getClass()) {
				return ((EqualAndNotEqual) left)
						.NotEqualTo((EqualAndNotEqual) right);
			} else {
				Class<?> matchedClass = CalculateClassMatch
						.EqualAndNotEqualMatch(left, right);
				if (matchedClass == null)
					throw new ExecuteErrorException("The two values cannot be compared by '!='");

				// Left
				EqualAndNotEqual nLeft;
				if (left.getClass() != matchedClass) {
					try {
						nLeft = (EqualAndNotEqual) matchedClass.newInstance();
						nLeft.ParseFromString(((EqualAndNotEqual) left)
								.SaveValueToString());
					} catch (Throwable o) {
						throw new ExecuteErrorException("The two values cannot be compared by '!='");
					}
				} else
					nLeft = (EqualAndNotEqual) left;

				// Right
				EqualAndNotEqual nRight;
				if (right.getClass() != matchedClass) {
					try {
						nRight = (EqualAndNotEqual) matchedClass.newInstance();
						nRight.ParseFromString(((EqualAndNotEqual) right)
								.SaveValueToString());
					} catch (Throwable o) {
						throw new ExecuteErrorException("The two values cannot be compared by '!='");
					}
				} else
					nRight = (EqualAndNotEqual) right;

				return nLeft.NotEqualTo(nRight);
			}
		} else {
			throw new ExecuteErrorException("The two values cannot be compared by '!='");
		}
	}

	protected EvaluateExpr eeLeft, eeRight;
}
